package cn.touna.jss.modules.credit.service.impl;

import cn.touna.jss.common.util.CollectionUtil;
import cn.touna.jss.modules.credit.entity.CarNumberDictEntity;
import cn.touna.jss.modules.credit.entity.CarNumberDictEntityExample;
import cn.touna.jss.modules.credit.mapper.CarNumberDictEntityMapper;
import cn.touna.jss.modules.credit.service.CarNumberDictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author:wuwc
 * @Description:
 * @Date:2018/5/9 11:58
 */
@Service
public class CarNumberDictServiceImpl implements CarNumberDictService {

    @Autowired
    private CarNumberDictEntityMapper carNumberDictEntityMapper;

    @Override
    public int saveCarNumberDict(CarNumberDictEntity carNumberDictEntity) {
        carNumberDictEntityMapper.insertSelective(carNumberDictEntity);
        return 1;
    }

    @Override
    public void saveCarNumberDict(List<CarNumberDictEntity> carNumberDictEntityList) {
        List<CarNumberDictEntity> dbCarNumberDictList = queryCarNumberDict(null);
        Collection<CarNumberDictEntity> result = new LinkedList();
        result = CollectionUtil.getDiff(carNumberDictEntityList, dbCarNumberDictList);
        if (!CollectionUtils.isEmpty(result)) {
            carNumberDictEntityMapper.batchInsert(result);
        }
    }

    @Override
    public List<CarNumberDictEntity> queryCarNumberDict(String carNumberPrefix) {
        CarNumberDictEntityExample ex = new CarNumberDictEntityExample();
        if (StringUtils.isNotBlank(carNumberPrefix)) {
            ex.or().andCarNumberPrefixEqualTo(carNumberPrefix);
        }
        List<CarNumberDictEntity> carNumberDictEntityList = carNumberDictEntityMapper.selectByExample(ex);
        if (!CollectionUtils.isEmpty(carNumberDictEntityList)) {
            return carNumberDictEntityList;
        }
        return new ArrayList<CarNumberDictEntity>();
    }


    public static void main(String[] args) {
        List<String> list1 = new ArrayList<String>();
        list1.add("A");
        list1.add("B");
        list1.add("C");
        list1.add("G");
        list1.add("F");

        List<String> list2 = new ArrayList<String>();
        /*list2.add("C");
        list2.add("B");
        //list2.add("D");
        list2.add("A");
        list2.add("E");
        list2.add("F");*/

        Collection result = CollectionUtil.getDiff(list1, list2);
        // 并集
        list1.addAll(list2);
        // 去重复并集
        list1.removeAll(list1);
        list1.addAll(list2);
        // 交集
        list1.retainAll(list2);
        // 差集
        list1.removeAll(list2);

    }
}
